package com.delong.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.delong.lease.common.exception.LeaseException;
import com.delong.lease.common.redis.constant.RedisConstant;
import com.delong.lease.common.result.ResultCodeEnum;
import com.delong.lease.common.utils.JwtUtil;
import com.delong.lease.model.entity.SystemUser;
import com.delong.lease.model.enums.BaseStatus;
import com.delong.lease.web.admin.service.LoginService;
import com.delong.lease.web.admin.service.SystemUserService;
import com.delong.lease.web.admin.vo.login.CaptchaVo;
import com.delong.lease.web.admin.vo.login.LoginVo;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private SystemUserService systemUserService;

    @Override
    public CaptchaVo getCaptcha() {
        //1、创建验证码图片的工具类
        SpecCaptcha specCaptcha = new SpecCaptcha(130,48,4);
        specCaptcha.setCharType(Captcha.TYPE_DEFAULT);
        //2、生成验证码图片的Base64和验证码正确字符串
        //base64
        String base64Img = specCaptcha.toBase64();
        //字符串
        String codeString = specCaptcha.text();
        //3、key：正确字符串 + uuidkey
        String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID().toString().replaceAll("-", "");
        //将key、value以及过期时间设置到redis中
        redisTemplate.opsForValue().set(key,codeString,RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);
        //4、uuidkey + 验证码图片 --》 CaptchaVo中
        return new CaptchaVo(base64Img, key);
    }

    @Override
    public String login(LoginVo loginVo) {
        //一、检查验证码
        //1、校验输入验证码是否为空
        String captchaCode = loginVo.getCaptchaCode();
        if (ObjectUtils.isEmpty(captchaCode)){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND);
        }
        //2、校验redis中验证码是否过期
        String codeString = (String) redisTemplate.opsForValue().get(loginVo.getCaptchaKey());
        if (ObjectUtils.isEmpty(codeString)){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
        //3、校验验证码是否和redis中的一致
        if (!codeString.equalsIgnoreCase(captchaCode)){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }
        //二、校验账号和密码以及账号状态
        //1、检查账号是否正确
        String username = loginVo.getUsername();
        LambdaQueryWrapper<SystemUser> systemUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        systemUserLambdaQueryWrapper.eq(SystemUser::getUsername, username);
        SystemUser systemUser = systemUserService.getOne(systemUserLambdaQueryWrapper);
        if (systemUser == null){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }
        //2、检查密码是否正确
        String password = DigestUtils.md5Hex(loginVo.getPassword());
        String userPassword = systemUser.getPassword();
        if (!userPassword.equals(password)){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
        //3、检查状态是否可用
        if (systemUser.getStatus() == BaseStatus.DISABLE){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }
        //三、生成token
        return JwtUtil.createToken(systemUser.getId(), systemUser.getUsername());
    }
}
